//
//  ViewController.m
//  ui18 _网络请求
//
//  Created by lirui on 15/12/30.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "netWorkHandler.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *img;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


//封装GET
- (IBAction)GET:(id)sender {
    
    [netWorkHandler GETWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141" completion:^(id result) {
        NSLog(@"%@",result);
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}
- (IBAction)asyncGET:(id)sender {
    NSLog(@"~~~~异步GET~~~~");
   // NSURLConnection
    //1.获取网址字符串
    NSString *str = @"http://pic13.nipic.com/20110313/2686941_211532129160_2.jpg";
    NSString *urlStr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    //2.nsstring -> URL
    NSURL *url =[  NSURL URLWithString:urlStr];
    //3.通过url创建请求对像(默认get请求)
    NSURLRequest  *request =[ NSURLRequest requestWithURL:url];
    //iOS9.0以后  使用NSURLSession
    NSURLSession  *session = [ NSURLSession sharedSession];
    //创建task对象
    //参数1 :请求
    //参数2 : 获取数据的block
    
    NSURLSessionDataTask *task =[ session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //block参数
        //1.请求到数据
        //2.响应
        //3.错误信息
        
        //异步请求在子线程中进行 数据操作需要返回主线程
        
        dispatch_queue_t main = dispatch_get_main_queue();
        //在主线程中执行
        dispatch_async(main, ^{
            
            self.img.image = [ UIImage imageWithData:data];
        });
    }];
    //任务执行
    [task resume];
    
}

- (IBAction)asyncPOST:(id)sender {
    NSLog(@"~~~~~异步POST~~~~~~~");
    
    NSString *str =@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    NSString *urlStr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[ NSCharacterSet characterSetWithCharactersInString:str]];
    NSURL *url = [NSURL URLWithString:urlStr ];
    NSMutableURLRequest *request =[ NSMutableURLRequest  requestWithURL:url];
    request.HTTPMethod=@"POST";
    //设置body
    NSString *body =@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
    request.HTTPBody =[ body dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURLSession *session =[ NSURLSession sharedSession];
    
    NSURLSessionDataTask  *task =[ session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            id result =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", result);
            
        });
    }];
    
    
    
    
    [task resume];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
